<template>
  <a-layout id="components-layout-demo-custom-trigger" class="wrapImg">
    <a-layout-header class="headerWrap">
      <div class="logoName">
        <img style="margin-right: 5px; width: 40px" src="../../src/assets/logo.png" />
        {{ title }}
      </div>
      <a-tabs class="root-tabs" v-model="rootActive" size="large" @change="rootMenuChange" :style="{ flex: 1, 'margin-left': '40px', width:'calc(100% - 650px)' }">
        <a-tab-pane v-for="item in menus" :key="item.raw.code">
          <template #tab>
            <img v-if="!!filterMenuIcon(item.raw.code)" :src="filterMenuIcon(item.raw.code)" :style="{ width: '20px', height: '20px', 'margin-bottom': '5px' }" />
            {{ item.raw.name }}
          </template>
        </a-tab-pane>
      </a-tabs>
      <div class="userHeader">
        <div class="icon-wrapper base-btn" @click="jumpRouter">
           <BaseSvgIcon class="icon-item userHImg" icon="homepage" color="red" />
        </div>
        <post-select :popover-visible.sync="popoverVisible" />
        <AvatarDown>
          <MessageIcon slot="message" />
        </AvatarDown>
      </div>
    </a-layout-header>
    <a-layout
      :style="{
        transition: '.3s ease',
        height: 'calc('+GET_REDRESS_PERCENTAGE+'vh - 64px)',
        background: 'none',
        overflowX: 'hidden',
        display: 'flex'
      }"
    >
      <a-layout-sider
        :style="{
          position: 'relative',
          width: menuWidth,
          maxWidth: menuWidth,
          minWidth: menuWidth,
          height: '100%',
          background: 'transparent'
        }"
      >
        <menu-basic :rootActive="rootActive" @switchSecondaryMenu="switchSecondaryMenu" :collapsed="collapsed" />
      </a-layout-sider>
      <a-layout-content class="main">
        <multi-tab2 :style="{ 'margin-left': '20px' }" />
        <div style="position: relative;height:100%;">
          <transition name="fade-transform" mode="out-in" @before-enter="beforeEnter" @after-enter="afterEnter" v-show="!isIframe">
                <router-view  />
          </transition>
          <!-- iframe -->
              <component v-for="item in hasOpenComponentsArr" :key="item.name" :is="item.component" v-show="isIframe && $route.path === item.path"></component>
          <!-- iframe -->
        </div>
      </a-layout-content>
    </a-layout>
  </a-layout>
</template>
<script>
import { mapState, mapGetters } from 'vuex'
import MenuBasic from '@/layouts/MenuBasicV3'
import MultiTab2 from '@/components/MultiTab2'
import AvatarDown from '@/components/avatarDown/avatarDown'
import MessageIcon from './messageIcon.vue'
import PostSelect from './PostSelect.vue'
import setting from '@/config/defaultSettings'
import { getMenuIcon } from '@/utils/util.js'
import BaseSvgIcon from '@/components/BaseSvgIcon'

export default {
  data() {
    return {
      // 登录时初始菜单状态为收缩
      collapsed: localStorage.getItem('initCollapsed') === 'true' ? true : localStorage.getItem('indexCollapsed') === 'true',
      noCache: ['materialMasterPlanIndex'],
      popoverVisible: false,
      timer: 0,
      iframeArr: [],
      componentsArr: [],
      title: setting.title,
      rootActive: '',
      // 是否内联挂框架
      isIframe: false,
      // 页面key
      pageKey: '',
      // 缓存iframe路由模块名
      routeNames: ['jixiao','enterprisePlanning']
    }
  },
  created() {
    // 缓存iframe路由
    let routes = this.$store.getters.addRouters.find((i) => i.path === '/')?.children?.filter((i) => this.routeNames.includes(i.name))
    routes.forEach((item)=>{
      this.getComponentsArr(item.children)
    })
    this.isOpenIframePage()
  },
  watch: {
    $route(val) {
      this.isOpenIframePage()
      this.pageKey = val.path
    },
    activeCodes: {
      handler(val){
        this.rootActive = val[0] || this.menus[0]?.raw.code
      },
      deep: true
    }
  },
  components: {
    MultiTab2,
    AvatarDown,
    MenuBasic,
    MessageIcon,
    PostSelect
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      if (from.path === '/login') {
        vm.popoverVisible = true
        vm.timer = setTimeout(() => {
          vm.popoverVisible = false
          clearTimeout(vm.timer)
        }, 10000)
      }
    })
  },
  methods: {
    filterMenuIcon(code) {
      return getMenuIcon(code)
    },
    // 扁平化路由
    flatRouters(list) {
      let routers = []
      function getList(e) {
        for (let item of e) {
          routers.push(item)
          if (item.children?.length) {
            getList(item.children)
          }
        }
      }
      getList(list)
      return routers
    },
    // 是否为iframe页面
    isOpenIframePage() {
      const target = this.componentsArr.find((item) => {
        return item.path === this.$route.path
      })
      this.isIframe = !(target === undefined)
      if (target && !target.hasOpen) {
        target.hasOpen = true
      }
    },
    // 筛选需要缓存的iframe页面
    getComponentsArr(routes) {
      routes && routes.forEach((it) => {
        if (it.iframeComponent) {
          const name = it.name || it.path.replace('/', '')

          this.componentsArr.push({
            name: name,

            path: it.path,

            hasOpen: false,

            component: it.iframeComponent
          })
        }

        if (it.children && it.children.length > 0) {
          this.getComponentsArr(it.children)
        }
      })
    },
    beforeEnter(el) {
      this.$nextTick(() => {
        el.parentNode && (el.parentNode.style.overflow = 'hidden')
      })
    },
    afterEnter(el) {
      this.$nextTick(() => {
        el.parentNode && (el.parentNode.style.overflow = 'auto')
      })
    },
    switchSecondaryMenu(showSecondaryMenu) {
      this.collapsed = !showSecondaryMenu
      localStorage.setItem('indexCollapsed',this.collapsed)
    },
    rootMenuChange(val) {
      this.rootActive = val
    },
    // 回到门户页面
    jumpRouter() {
      this.$router.push('/gateway')
    }
  },
  mounted() {
    this.rootActive = this.activeCodes[0] || this.menus[0]?.raw.code
  },
  computed: {
    ...mapGetters(['GET_REDRESS_PERCENTAGE']),
    ...mapState({
      // 动态主路由
      mainMenu: (state) => state.permission.addRouters,
      menus: (state) => state.user.menus,
      activeCodes: state => state.user.activeCodes
    }),
    menuWidth() {
      return this.collapsed ? '80px' : '280px'
    },
    style() {
      return {
        minWidth: '1200px',
        height: '100%',
        overflowY: 'auto',
        overflowX: 'hidden'
      }
    },
    // 懒加载缓存的iframe页面
    hasOpenComponentsArr() {
      return this.componentsArr.filter((item) => item.hasOpen && this.$store.state.tabInfo.pathMap.some((i) => i.path === item.path))
    }
  }
}
</script>
<style lang="less">
#components-layout-demo-custom-trigger {
  .logoName {
    padding: 0 20px;
    font-size: 18px;
    color: white;
    font-weight: bold;
    cursor: default;
  }
  .trigger {
    font-size: 18px;
    line-height: 64px;
    padding: 0 24px;
    cursor: pointer;
    transition: color 0.3s;
  }

  .trigger:hover {
    color: #1890ff;
  }

  .logo {
    height: 32px;
    background: rgba(255, 255, 255, 0.2);
    margin: 16px;
  }

  .main {
    position: relative;
    height: 100%;
    width: 100%;
    overflow: hidden;

    .ant-pro-page-header-wrap {
      .ant-pro-page-header-wrap-page-header-warp {
        background: none;

        .ant-pro-grid-content {
          .ant-page-header.has-breadcrumb {
            padding: 12px 10px;

            .ant-breadcrumb > span:last-child {
              color: #333333;
            }
          }
        }
      }
    }

    .ant-pro-page-header-wrap > .ant-pro-grid-content {
      /*position: relative;*/
      /*height: 100%;*/
      /*width: 100%;*/
      /*overflow: auto;*/

      .ant-pro-page-header-wrap-children-content {
        padding: 0 10px;
        margin: 0;
      }
    }
  }

  .ant-tabs-bar.ant-tabs-top-bar.ant-tabs-card-bar {
    background: none !important;
    border: none !important;
    /*padding-left: 10px!important;*/
    padding-left: 0px !important;
    overflow: hidden;
  }

  .headerWrap {
    position: relative;
    background: #387fc6;
    padding: 0;
    width: 100%;
    display: flex;
    // .root-tabs{
    //   flex: 1;
    //   margin: 0 0 0 40px;
    //   font-size: 20px;
    //   color: white;
    //   display: flex;
    //   height: 100%;
    //   >li{
    //     position: relative;
    //     height: 100%;
    //     padding: 0 20px;
    //     cursor: pointer;
    //   }
    //   >li+li{
    //     margin-left: 20px;
    //   }
    //   >li.active::after{
    //     position: absolute;
    //     left: 0;
    //     bottom: 10px;
    //     width: 0;
    //     height: 0;
    //     border-left: 5px solid transparent;
    //     border-right: 5px solid transparent;
    //     border-bottom: 10px solid white;
    //   }
    // }
    .ant-tabs-bar {
      border-bottom: none;
    }
    .ant-tabs-nav-container {
      font-size: 20px !important;
      color: rgba(255, 255, 255, 0.65);
    }
    .ant-tabs-tab-active {
      color: white;
    }
    .ant-tabs-ink-bar {
      position: relative;
      height: 10px;
      background: none;
      bottom: 10px;
    }
    .ant-tabs-ink-bar::after {
      content: '';
      position: absolute;
      width: 0;
      height: 0;
      border-left: 5px solid transparent;
      border-right: 5px solid transparent;
      border-bottom: 5px solid white;
      left: 50%;
      margin-left: -3px;
    }
    .userHeader {
      display: flex;
      justify-content: flex-end;
      width: 410px;
      padding-right: 20px;
      color: #ffffff;
      text-align: center;
    }

    .ant-pro-multi-tab {
      position: absolute;
      bottom: 0;
      left: 0;
      width: 100%;
      /*margin: -37px 0 0;*/
      z-index: 100;
      background: none;

      .ant-tabs-nav-container {
        height: 48px;

        .ant-tabs-nav-wrap {
          .ant-tabs-nav-scroll {
            padding: 0 10px;
          }
        }
      }

      .ant-pro-multi-tab-wrapper {
        padding-left: 0;

        .ant-tabs-tab-prev,
        .ant-tabs-tab-next {
          background-color: #ffffff;
          border-right: 1px solid #e8e8e8;
          border-left: 1px solid #e8e8e8;
          border-top-left-radius: 4px;
          border-top-right-radius: 4px;
        }

        .ant-tabs-nav-wrap {
          .ant-tabs-tab {
            height: 48px;
            line-height: 48px;
            background: none;
            color: #ffffff;
            border: none;
            background: rgba(0, 0, 0, 0.4);
            padding-left: 44px;
            padding-right: 44px;
            border-radius: 4px 4px 0px 0px;
          }

          .ant-tabs-tab.ant-tabs-tab-active {
            background: #ffffff;
            color: #1890ff;
            border-color: #e8e8e8;
            border-bottom: 1px solid #ffffff;
            border-top-left-radius: 4px;
            border-top-right-radius: 4px;
          }
        }
      }
    }
  }
  .userHImg {
    margin-right: 12px;
    width: 24px;
    height: 24px;
    transform: translateY(4px);
  }
}
</style>
